package com.gpframework.module.controller.order;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.gpframework.common.enums.YesNoEnum;
import com.gpframework.module.order.domain.GpFlowConfigRoleRela;
import com.gpframework.module.order.domain.req.GpFlowConfigRoleRelaAdd;
import com.gpframework.module.order.domain.vo.GpFlowConfigRoleRelaVo;
import com.gpframework.module.order.service.IGpFlowConfigRoleRelaService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.module.order.domain.GpFlowConfig;
import com.gpframework.module.order.domain.vo.GpFlowConfigVo;
import com.gpframework.module.order.domain.req.GpFlowConfigReq;
import com.gpframework.module.order.domain.req.GpFlowConfigAdd;
import com.gpframework.module.order.domain.req.GpFlowConfigUpdate;
import com.gpframework.module.order.service.IGpFlowConfigService;
import com.gpframework.common.utils.poi.ExcelUtil;
import javax.validation.Valid;
import io.swagger.annotations.*;
import com.gpframework.common.core.page.TableDataInfo;
/**
 * @ClassName GpFlowConfigController
 * @Author gpframework
 * @Date 2023-06-16
 * @Version 1.0.0
 * @Description 流转节点配置Controller
 */
@Validated
@Api(tags = "流转节点配置")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/order/flowConfig" )
public class GpFlowConfigController extends BaseController {

    private final IGpFlowConfigService iGpFlowConfigService;
    private final IGpFlowConfigRoleRelaService iGpFlowConfigRoleRelaService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2023-06-16
  * @Param gpFlowConfig
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('order:flowConfig:list')")
    @GetMapping("/list")
    public TableDataInfo<GpFlowConfigVo> list(GpFlowConfigReq gpFlowConfigReq)
    {
        startPage();
        QueryWrapper<GpFlowConfig> lqw = new QueryWrapper<GpFlowConfig>();
        gpFlowConfigReq.generatorQuery(lqw,true);
        lqw.groupBy("gp.id");
        List<GpFlowConfigVo> list = iGpFlowConfigService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2023-06-16
     * @Param gpFlowConfig
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('order:flowConfig:add')" )
    @Log(title = "添加流转节点配置" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpFlowConfigAdd gpFlowConfigAdd) {
        GpFlowConfig gpFlowConfig = Convert.convert(new TypeReference<GpFlowConfig>() {}, gpFlowConfigAdd);
        boolean save = iGpFlowConfigService.save(gpFlowConfig);
        if (save) {
            /*List<GpFlowConfigRoleRelaAdd> flowConfigRoleRelaAddList = gpFlowConfigAdd.getFlowConfigRoleRelaList();
            for (GpFlowConfigRoleRelaAdd gpFlowConfigRoleRelaAdd : flowConfigRoleRelaAddList) {
                gpFlowConfigRoleRelaAdd.setConfigId(gpFlowConfig.getId());
            }*/
            List<GpFlowConfigRoleRelaAdd> flowConfigRoleRelaAddList = new ArrayList<>();
            GpFlowConfigRoleRelaAdd gpFlowConfigRoleRelaAdd;
            for (Long roleId : gpFlowConfigAdd.getRoleIds()) {
                gpFlowConfigRoleRelaAdd = new GpFlowConfigRoleRelaAdd();
                gpFlowConfigRoleRelaAdd.setConfigId(gpFlowConfig.getId());
                gpFlowConfigRoleRelaAdd.setRoleId(roleId);
                flowConfigRoleRelaAddList.add(gpFlowConfigRoleRelaAdd);
            }
            List<GpFlowConfigRoleRela> flowConfigRoleRelaList = Convert.convert(new TypeReference<List<GpFlowConfigRoleRela>>() {}, flowConfigRoleRelaAddList);
            iGpFlowConfigRoleRelaService.saveBatch(flowConfigRoleRelaList);
        }
        return toAjax(save ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2023-06-16
     * @Param gpFlowConfig
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('order:flowConfig:edit')" )
    @Log(title = "修改流转节点配置" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GpFlowConfigUpdate gpFlowConfigUpdate) {
        GpFlowConfig gpFlowConfig = Convert.convert(new TypeReference<GpFlowConfig>() {}, gpFlowConfigUpdate);
        boolean update = iGpFlowConfigService.updateById(gpFlowConfig);
        if (update) {
            iGpFlowConfigRoleRelaService.remove(new QueryWrapper<GpFlowConfigRoleRela>().eq("config_id", gpFlowConfig.getId()).eq("del_status", YesNoEnum.NO.getKey()));
            /*List<GpFlowConfigRoleRelaAdd> flowConfigRoleRelaAddList = gpFlowConfigUpdate.getFlowConfigRoleRelaList();
            for (GpFlowConfigRoleRelaAdd gpFlowConfigRoleRelaAdd : flowConfigRoleRelaAddList) {
                gpFlowConfigRoleRelaAdd.setConfigId(gpFlowConfig.getId());
            }*/
            List<GpFlowConfigRoleRelaAdd> flowConfigRoleRelaAddList = new ArrayList<>();
            GpFlowConfigRoleRelaAdd gpFlowConfigRoleRelaAdd;
            for (Long roleId : gpFlowConfigUpdate.getRoleIds()) {
                gpFlowConfigRoleRelaAdd = new GpFlowConfigRoleRelaAdd();
                gpFlowConfigRoleRelaAdd.setConfigId(gpFlowConfig.getId());
                gpFlowConfigRoleRelaAdd.setRoleId(roleId);
                flowConfigRoleRelaAddList.add(gpFlowConfigRoleRelaAdd);
            }
            List<GpFlowConfigRoleRela> flowConfigRoleRelaList = Convert.convert(new TypeReference<List<GpFlowConfigRoleRela>>() {}, flowConfigRoleRelaAddList);
            iGpFlowConfigRoleRelaService.saveBatch(flowConfigRoleRelaList);
        }
        return toAjax(update ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2023-06-16
     * @Param gpFlowConfig
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('order:flowConfig:export')" )
    @Log(title = "导出流转节点配置" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(GpFlowConfigReq gpFlowConfigReq) {
        QueryWrapper<GpFlowConfig> lqw = new QueryWrapper<GpFlowConfig>();
        gpFlowConfigReq.generatorQuery(lqw,true);
        List<GpFlowConfigVo> list = iGpFlowConfigService.listVo(lqw);
        ExcelUtil<GpFlowConfigVo> util = new ExcelUtil<GpFlowConfigVo>(GpFlowConfigVo. class);
        return util.exportExcel(list, "flowConfig" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2023-06-16
     * @Param gpFlowConfig
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('order:flowConfig:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpFlowConfigVo> getInfo(@PathVariable("id" ) Long id) {
        GpFlowConfigVo vo = iGpFlowConfigService.getVoById(id);
        vo.setFlowConfigRoleRelaVoList(iGpFlowConfigRoleRelaService.listVo(new QueryWrapper<GpFlowConfigRoleRela>().eq("config_id", vo.getId()).eq("del_status", YesNoEnum.NO.getKey())));
        vo.setRoleIds(vo.getFlowConfigRoleRelaVoList().stream().map(GpFlowConfigRoleRelaVo::getRoleId).collect(Collectors.toList()));
        return AjaxResult.success(vo);
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2023-06-16
     * @Param gpFlowConfig
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('order:flowConfig:remove')" )
    @Log(title = "删除流转节点配置" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpFlowConfigService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
